<template>
  <div class="test-container">
    <!-- 头部查询框 -->
    <vab-query-form>
      <vab-query-form-left-panel>
        <el-button icon="el-icon-plus" type="primary" @click="handleAdd">
          添加
        </el-button>
        <el-button icon="el-icon-delete" type="danger" @click="handleDelete">
          删除
        </el-button>
      </vab-query-form-left-panel>
      <vab-query-form-right-panel>
        <el-form ref="form" :model="queryForm" :inline="true" @submit.native.prevent>
          <el-form-item style="width: 160px;">
            <el-input v-model="queryForm.paramMap.chapterName" placeholder="章节" clearable />
          </el-form-item>

          <el-form-item>
            <el-button icon="el-icon-search" type="primary" native-type="submit" @click="handleQuery">
              查询
            </el-button>
          </el-form-item>
        </el-form>
      </vab-query-form-right-panel>
    </vab-query-form>

    <!-- 列表 -->
    <el-table ref="tableSort" v-loading="listLoading" :data="chapterList" :element-loading-text="elementLoadingText"
      :height="height" @selection-change="setSelectRows">
      <el-table-column show-overflow-tooltip type="selection" width="80"></el-table-column>
      <el-table-column show-overflow-tooltip label="序号" width="120">
        <template #default="scope">
          {{ scope.$index + 1 }}
        </template>
      </el-table-column>
      <el-table-column show-overflow-tooltip prop="chapterName" label="章节"></el-table-column>
      <el-table-column show-overflow-tooltip label="操作" width="260px">
        <template #default="{ row }">
          <el-button type="text" @click="handleEdit(row)">编辑</el-button>
          <el-button type="text" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <el-pagination :background="background" :current-page="queryForm.current" :layout="layout"
      :page-size="queryForm.size" :total="total" @current-change="handleCurrentChange" @size-change="handleSizeChange">
    </el-pagination>


    <!-- 编辑弹窗 -->
    <div>
      <el-dialog :title="title" :visible.sync="dialogFormVisible" width="500px">
        <el-form :model="editForm">
          <el-form-item label="章节名称" :label-width="formLabelWidth">
            <el-input v-model="editForm.chapterName" autocomplete="off"></el-input>
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取 消</el-button>
          <el-button type="primary" @click="saveEdit">确 定</el-button>
        </div>
      </el-dialog>
    </div>
  </div>
</template>
<script>
  import {
    listTaskChapter,
    saveTaskChapter,
    updateTaskChapter,
    deleteTaskChapter,
  } from '@/api/task.js'
  export default {
    name: 'ChapterIndex',
    data() {
      return {
        chapterList: [],
        listLoading: true,
        layout: 'total, sizes, prev, pager, next, jumper',
        total: 0,
        background: true,
        selectRows: '',
        elementLoadingText: '正在加载...',
        queryForm: {
          current: 1,
          size: 10,
          paramMap: {
            schoolId: ''
          },
        },
        dialogFormVisible: false,
        formLabelWidth: '120px',
        title: '',
        editForm:{}
      }
    },
    computed: {
      height() {
        return this.$baseTableHeight()
      },
    },
    created() {
      //学院ID
      window.sessionStorage.setItem("schoolId", "1440285428262379522");
      this.queryForm.paramMap.schoolId = window.sessionStorage.getItem("schoolId");
      this.chapterData()
    },
    mounted() {},
    methods: {
      saveEdit(){
        this.dialogFormVisible = false
        if (this.title == '章节新增') {
          console.log('章节新增')
          this.editForm.schoolId = this.queryForm.paramMap.schoolId
          saveTaskChapter(this.editForm).then(data => {
            if (data.code == 200) {
              this.$baseMessage(data.msg, 'success')
            } else {
              this.$baseMessage(data.msg, 'error')
            }
            this.chapterData()
          })
        } else {
          console.log('章节编辑')
          updateTaskChapter(this.editForm).then(data => {
            if (data.code == 200) {
              this.$baseMessage(data.msg, 'success')
            } else {
              this.$baseMessage(data.msg, 'error')
            }
            this.chapterData()
          })
        }
      },
      handleAdd() {
        this.title = '章节新增'
        this.editForm = {}
        this.dialogFormVisible = true
      },
      handleEdit(row) {
        this.title = '章节编辑'
        this.editForm = row
        this.dialogFormVisible = true
      },
      handleDelete(row) {
        if (row.chapterId) {
          this.$baseConfirm('你确定要删除当前项吗', null, async () => {
            const {
              msg
            } = await deleteTaskChapter({
              ids: [row.chapterId],
            })
            this.$baseMessage(msg, 'success')
            if(this.chapterList.length==1){
              this.queryForm.current = parseInt(this.queryForm.current) - 1
            }
            this.chapterData()
          })
        } else {
          if (this.selectRows.length > 0) {
            //const ids = this.selectRows.map((item) => item.chapterId).join()
            const ids = []
            for (let i in this.selectRows) {
              ids.push(this.selectRows[i].chapterId)
            }
            this.$baseConfirm('你确定要删除选中项吗', null, async () => {
              const {
                msg
              } = await deleteTaskChapter({
                ids: ids,
              })
              this.$baseMessage(msg, 'success')
              if(this.selectRows.length==this.chapterList.length){
                this.queryForm.current = parseInt(this.queryForm.current) - 1
              }
              this.chapterData()
            })
          } else {
            this.$baseMessage('未选中任何行', 'error')
            return false
          }
        }
      },
      handleSizeChange(val) {
        this.queryForm.size = val
        this.chapterData()
      },
      handleCurrentChange(val) {
        this.queryForm.current = val
        this.chapterData()
      },
      handleQuery() {
        this.queryForm.current = 1
        this.chapterData()
      },
      setSelectRows(val) {
        this.selectRows = val
      },
      async chapterData() {
        //this.listLoading = true
        const {
          data
        } = await listTaskChapter(this.queryForm)
        console.log(data)
        this.chapterList = data.records
        this.total = parseInt(data.total)
        setTimeout(() => {
          this.listLoading = false
        }, 500)
      },
    },
  }
</script>
